monadsphere = {};
monadsphere.ids = {};

monadsphere.isIE = window.navigator.appName == 'Microsoft Internet Explorer'

monadsphere.remoteCall = function(callName, data) {
    var path = window.location.pathname;
    callName = path.substring(0, path.lastIndexOf('/') + 1) + callName;
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
        monadsphere.processResponse(httpRequest);
    };
    if (typeof (data) == 'undefined' || data == null) {
        httpRequest.open('GET', callName, true);
        httpRequest.send(null);
    } else {
        httpRequest.open('POST', callName, true);
        httpRequest.send(JSON.stringify(data));
    }
}

monadsphere.processResponse = function(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            eval(httpRequest.responseText);
        } else {
            alert('There is a problem with the request:' + httpRequest.status);
        }
    }
}

monadsphere.loadScript = function(scriptName, async, callback) {
    var path = window.location.pathname;
    scriptName = path.substring(0, path.lastIndexOf('/') + 1) + scriptName;
    var httpRequest = new XMLHttpRequest();
    if (async) {
        httpRequest.onreadystatechange = function() {
            monadsphere.processCallback(httpRequest, callback);
        }
    }
    httpRequest.open('GET', scriptName += "?" + (new Date()).getTime(), async);
    httpRequest.send(null);
    if (!async) {
        monadsphere.processCallback(httpRequest, callback)
    }
}

monadsphere.processCallback = function(httpRequest, callback) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            eval(httpRequest.responseText);
            if (typeof (callback) == "string" && callback.length > 0) {
                eval(callback)
            } else if (typeof (callback) == "function") {
                callback();
            }
        } else {
            alert('There is a problem with the callback:' + httpRequest.status);
        }
    }
}

monadsphere.loadCss = function(cssName) {
    var path = window.location.pathname;
    cssName = path.substring(0, path.lastIndexOf('/') + 1) + cssName;
    var head0 = document.getElementsByTagName("head")[0];
    var css = document.createElement('link');
    css.type = 'text/css';
    css.rel = 'stylesheet';
    css.href = cssName;
    css.media = 'screen';
    head0.appendChild(css);
}
